<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>搜索</title>
    <%- include('../../_global/baseLibs') %>
</head>
<body>
<section class="page-wrap search-box">
    <header class="header-search fixed">
        <aside class="header-content">
            <div class="search-bar-box">
                <a class="input-search-box" href="#">
                    <input class="input-search" placeholder="输入医院名称" />
                    <i class="fdj-icon"></i>
                </a>
                <a href="javascript:void(0);" onclick="searchbut()" class="qd-text">确定</a>
                <a class="qx-text" href="javascript:history.back()">取消</a>
            </div>
        </aside>
    </header>
    <div class="search-history-box">
        <span class="text-left">搜索历史</span>
        <a class="qc-text">清除</a>
    </div>
    <div class="search-history-list">
        <ul>
        </ul>
    </div>
</section>
</body>
    <script type="text/html" id="searchCon">
        <li>
            <a href="javascript:void(0);">
                <i class="clock-icon"></i>
                <span></span>
                <div class="close-icon-out">
                    <i class="close-icon"></i>
                </div>
            </a>
        </li>
    </script>
<script>
    $(function(){
        initdata();
        initView();
    });
    //加载点击事件
    function initView(){
        $(".close-icon-out").bind('click',function(e){
            e.preventDefault();
            var text = $(this).parent().find("span").html();
            var cache = StoreCache.getCache("searchCache");
            cache = $.grep(cache,function(num,index){
                if(num!=text)
                    return num;
            });
            $(this).parent().remove();
            StoreCache.setCache("searchCache",cache);
        });

        $(".qc-text").click(function(){
            StoreCache.removeCache("searchCache");
            $(".search-history-list ul").remove();
        });

        $(".search-history-list").find("a").bind('click',function(){
            var text = $(this).find("span").html();
            addCache(text);
            StoreCache.setCache("searchText",text);
            $.pageSwitch.goToUrl("search","searchResult");
        });
    }
    //加载历史记录
    function initdata(){
        var ary = StoreCache.getCache("searchCache");
        $(ary).each(function (index,item){
            var search = $($("#searchCon").html());
            search.find("span").html(item);
            $(".search-history-list ul").append(search);
        });
    }

    //添加历史记录
    function addCache(text){
        var cache = StoreCache.getCache("searchCache");
        var ary = [];
        ary.push(text);
        if(!StringUtils.isEmpty(cache)){
            cache = $.grep(cache, function(num,index){
                if(num != text){
                    return num;
                }
            });
            $(cache).each(function(index){
                if(index<4){
                    ary.push(this);
                }
            });
        }
        StoreCache.setCache("searchCache", ary);
    }
    function searchbut(){
        var text = $(".input-search").val();
        if(!StringUtils.isEmpty(text)) {
            addCache(text);
            StoreCache.setCache("searchText",text);
            $.pageSwitch.goToUrl("search","searchResult");
        }
    }
</script>
</html>